/*
============================================================================
 Name        : Tabbar.cpp
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : CTabbar implementation
============================================================================
*/

#include "Tabbar.h"
#include "Global.h"
#include "eStockDef.h"
#include "block.h"
#include "eStockAppUi.h"
#include "Titlebar.h"

CTabbar::CTabbar():
iFocusIndex(-1), iSelectedIndex(-1)
{
}

CTabbar::~CTabbar()
{
	iTabArray.Close();
//	CEikonEnv::Static()->ScreenDevice()->ReleaseFont(iFont);
}

CTabbar* CTabbar::NewLC()
{
	CTabbar* self = new (ELeave)CTabbar();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

CTabbar* CTabbar::NewL()
{
	CTabbar* self=CTabbar::NewLC();
	CleanupStack::Pop(); // self;
	return self;
}

void CTabbar::ConstructL()
{
}

void CTabbar::Draw(CFbsBitGc& g)
{
    TInt tabCount = iTabArray.Count();
	if (tabCount > 0)
    {
        CGraphicsDevice* screenDevice = CEikonEnv::Static()->ScreenDevice();

        TInt maxTabTextLength = 0;
        for (TInt i = 0; i < tabCount; ++i)
            if (maxTabTextLength < iTabArray[i].iText.Length())
                maxTabTextLength = iTabArray[i].iText.Length();
        TFontSpec myFontSpec = CEikonEnv::Static()->NormalFont()->FontSpecInTwips();
        if ((m_rect.Width() / tabCount - 6) / maxTabTextLength > m_rect.Height() - 12)
            myFontSpec.iHeight = screenDevice->VerticalPixelsToTwips(m_rect.Height() - 12);
        else
            myFontSpec.iHeight = screenDevice->HorizontalPixelsToTwips((m_rect.Width() / tabCount - 6) / maxTabTextLength);
        screenDevice->GetNearestFontInTwips(iFont, myFontSpec);
	    
		g.SetBrushStyle(CGraphicsContext::ESolidBrush);
		g.SetPenStyle(CGraphicsContext::ENullPen);
		g.SetBrushColor(TRgb(41, 46, 49));
		g.DrawRect(m_rect);
		g.SetPenStyle(CGraphicsContext::ESolidPen);

		TInt labels = iTabArray.Count();
		TInt buttonWidth = m_rect.Width() / labels;
		TRect labelRect(m_rect.iTl, TSize(buttonWidth, m_rect.Height()));

		g.UseFont(iFont);
		for (TInt i = 0; i < labels; ++i)
		{
			TRect tempRect = labelRect;
			tempRect.Shrink(TSize(2, 2));
			TInt baseLine = tempRect.Height() / 2 + iFont->AscentInPixels() - iFont->HeightInPixels() / 2;

			g.SetBrushStyle(CGraphicsContext::ENullBrush);
			if (i == iFocusIndex)
				CBaseBlock::DrawImageRect(g, tempRect,TSize(4, 4), 6);
			else if (i == iSelectedIndex)
				CBaseBlock::DrawImageRect(g, tempRect,TSize(4, 4), 5);
			else
				CBaseBlock::DrawImageRect(g, tempRect,TSize(4, 4), 2);

			g.SetPenColor(KRgbWhite);
			g.DrawText(iTabArray[i].iText, tempRect, baseLine, CGraphicsContext::ECenter);
			g.SetBrushStyle(CGraphicsContext::ESolidBrush);

			labelRect.Move(TPoint(buttonWidth, 0));
		}

		g.DiscardFont();
		screenDevice->ReleaseFont(iFont);
	}
}

TBool CTabbar::OnKeyDown(const TInt& keycode)
{
	if (iTabArray.Count() <= 0)
	{
		return EFalse;
	}
	if (keycode==CGlobal::Static()->g_nKeyLeft)
	{
		iFocusIndex--;
		if (iFocusIndex == -1)
			iFocusIndex = iTabArray.Count() - 1;
		return ETrue;
	} 
	else if (keycode==CGlobal::Static()->g_nKeyRight)
	{
		iFocusIndex++;
		if (iFocusIndex == iTabArray.Count())
			iFocusIndex = 0;
		return ETrue;
	}
	else if (keycode == CGlobal::Static()->g_nKeyDown)
	{
		SetFocus(EFalse);
		if(eStockUI->GetCurView() && eStockUI->GetCurView()->iContainer)
			eStockUI->GetCurView()->iContainer->m_block->ChangeFocus(ETrue, CGlobal::Static()->g_nKeyDown);
		return ETrue;
	}
	else if (keycode == CGlobal::Static()->g_nKeyUp)
	{
		SetFocus(EFalse);
		CeStockAppUi* pUi = STATIC_CAST(CeStockAppUi*,CEikonEnv::Static()->AppUi());
		CBaseView* pView = pUi->GetCurView();
		if(pView)
		{
			TUid id = pView->Id();
			if(id == KeStockMainGridViewId)
				BASECOMMON->iTitlebar->SetFocus(ETrue, CGlobal::Static()->g_nKeyUp);
			else
				BASECOMMON->iToolbar->SetFocus(ETrue, CGlobal::Static()->g_nKeyUp);
			return ETrue;
		}
		BASECOMMON->iTitlebar->SetFocus(ETrue, CGlobal::Static()->g_nKeyUp);
		return ETrue;
	}
	else if (keycode == CGlobal::Static()->g_nKeySelect)
	{
		TInt id = iTabArray[iFocusIndex].iId;
		iSelectedIndex = iFocusIndex;
		//iFocusIndex = -1;
		if (iObserver != NULL)
			iObserver->HandleTabbarCommand(id);
		return ETrue;
	}
	return EFalse;
}

void CTabbar::SetRect(const TInt& left,const TInt& top,const TInt& right, const TInt& bottom)
{
	CControl::SetRect(left,top,right,bottom);
}

TInt CTabbar::GetSelectedId()
{
	return iTabArray[iSelectedIndex].iId;
}

void CTabbar::SetSelectedId(TInt aId)
{
    if (aId == -1)
    {
        iSelectedIndex = aId;
        return;
    }
    
	TInt tabnum = iTabArray.Count();
	for (TInt i = 0; i < tabnum; ++i)
	{
		if (iTabArray[i].iId == aId)
		{
			iSelectedIndex = i;
			break;
		}
	}
}

TInt CTabbar::ButtonConatinsPt(TPoint aPt)
{
	TInt labels = iTabArray.Count();
	TInt buttonWidth = m_rect.Width() / labels;
	TRect labelRect(m_rect.iTl, TSize(buttonWidth, m_rect.Height()));

	for (TInt i = 0; i < labels; ++i)
	{
		if (labelRect.Contains(aPt))
			return i;
		labelRect.Move(TPoint(buttonWidth, 0));
	}
	return -1;
}

TBool CTabbar::OnMouseDown(const TPoint& aPt)
{
	if (m_rect.Contains(aPt))
	{
		TInt index = ButtonConatinsPt(aPt);
		if (index != -1)
			iFocusIndex = index;
		return ETrue;
	}
	return EFalse;
}

TBool CTabbar::OnMouseMove(const TPoint& aPt)
{
	if (m_rect.Contains(aPt))
	{
		TInt index = ButtonConatinsPt(aPt);
		if (index != -1)
			iFocusIndex = index;
		return ETrue;
	}
	return EFalse;
}

TBool CTabbar::OnMouseUp(const TPoint& aPt)
{
	if (m_rect.Contains(aPt))
	{
		TInt index = ButtonConatinsPt(aPt);
		if (index != -1 && (index == iFocusIndex))
		{
			TInt id = iTabArray[index].iId;
			iSelectedIndex = iFocusIndex;
			iFocusIndex = -1;
			if (iObserver != NULL)
				iObserver->HandleTabbarCommand(id);
		}
		return ETrue;
	}
	return EFalse;
}

void CTabbar::FocusChanged(TBool aFocus, const TInt keyCode)
{
    if (iTabArray.Count() <= 0 && aFocus)
    {
        iFocus = EFalse;
        if (keyCode == CGlobal::Static()->g_nKeyDown)
        {
        	if(eStockUI->GetCurView() && eStockUI->GetCurView()->iContainer)
            	eStockUI->GetCurView()->iContainer->m_block->ChangeFocus(ETrue, CGlobal::Static()->g_nKeyDown);
        }
        else if (keyCode == CGlobal::Static()->g_nKeyUp)
        {
        	CeStockAppUi* pUi = STATIC_CAST(CeStockAppUi*,CEikonEnv::Static()->AppUi());
			CBaseView* pView = pUi->GetCurView();
			if(pView)
			{
				TUid id = pView->Id();
				if(id == KeStockMainGridViewId)
					BASECOMMON->iTitlebar->SetFocus(ETrue, CGlobal::Static()->g_nKeyUp);
				else
					BASECOMMON->iToolbar->SetFocus(ETrue, CGlobal::Static()->g_nKeyUp);
				return;
			}
			BASECOMMON->iTitlebar->SetFocus(ETrue, CGlobal::Static()->g_nKeyUp);
        }
        return;
    }
    
	if (aFocus)
	{
	    if (iSelectedIndex == -1)
	        iFocusIndex = 0;
	    else
	        iFocusIndex = iSelectedIndex;
	}
	else
		iFocusIndex = -1;
}

void CTabbar::ResetTabArray()
{
	iTabArray.Reset();
}

void CTabbar::AppendTab(const TDesC& aText, TInt aId)
{
	iTabArray.Append(TTabLabel(aText, aId));
}
